home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Callback.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  3.3 KB  |  140 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Callback - a callback for receiving reactor events
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   use Net::DBus::Callback;
  28.  
  29.   # Assume we have a 'terminal' object and its got a method
  30.   # to be invoked everytime there is input on its terminal.
  31.   #
  32.   # To create a callback to invoke this method one might use
  33.   my $cb = Net::DBus::Callback->new(object => $terminal,
  34.                                     method => "handle_stdio");
  35.  
  36.  
  37.   # Whatever is monitoring the stdio channel, would then
  38.   # invoke the callback, perhaps passing in a parameter with
  39.   # some 'interesting' data, such as number of bytes available
  40.   $cb->invoke($nbytes)
  41.  
  42.   #... which results in a call to
  43.   #  $terminal->handle_stdio($nbytes)
  44.  
  45. =head1 DESCRIPTION
  46.  
  47. This module provides a simple container for storing details
  48. about a callback to be invoked at a later date. It is used
  49. when registering to receive events from the L<Net::DBus::Reactor>
  50. class
  51.  
  52. =head1 METHODS
  53.  
  54. =over 4
  55.  
  56. =cut
  57.  
  58. package Net::DBus::Callback;
  59.  
  60. use 5.006;
  61. use strict;
  62. use warnings;
  63.  
  64. =item my $cb = Net::DBus::Callback->new(method => $name, [args => \@args])
  65.  
  66. Creates a new callback object, for invoking a plain old function. The C<method>
  67. parameter should be the fully qualified function name to invoke, including the
  68. package name. The optional C<args> parameter is an array reference of parameters
  69. to be pass to the callback, in addition to those passed into the C<invoke> method.
  70.  
  71. =item my $cb = Net::DBus::Callback->new(object => $object, method => $name, [args => \@args])
  72.  
  73. Creates a new callback object, for invoking a method on an object. The C<method>
  74. parameter should be the name of the method to invoke, while the C<object> parameter
  75. should be a blessed object on which the method will be invoked. The optional C<args> 
  76. parameter is an array reference of parameters to be pass to the callback, in addition 
  77. to those passed into the C<invoke> method.
  78.  
  79. =cut
  80.  
  81. sub new {
  82.     my $proto = shift;
  83.     my $class = ref($proto) || $proto;
  84.     my %params = @_;
  85.     my $self = {};
  86.  
  87.     $self->{object} = $params{object} ? $params{object} : undef;
  88.     $self->{method} = $params{method} ? $params{method} : die "method parameter is required";
  89.     $self->{args} = $params{args} ? $params{args} : [];
  90.  
  91.     bless $self, $class;
  92.  
  93.     return $self;
  94. }
  95.  
  96. =item $cb->invoke(@args)
  97.  
  98. Invokes the callback. The argument list passed to the callback
  99. is a combination of the arguments supplied in the callback
  100. constructor, followed by the arguments supplied in the C<invoke>
  101. method.
  102.  
  103. =cut
  104.  
  105. sub invoke {
  106.     my $self = shift;
  107.     
  108.     if ($self->{object}) {
  109.     my $obj = $self->{object};
  110.     my $method = $self->{method};
  111.  
  112.     $obj->$method(@{$self->{args}}, @_);
  113.     } else {
  114.     my $method = $self->{method};
  115.  
  116.     &$method(@{$self->{args}}, @_);
  117.     }
  118. }
  119.  
  120. 1;
  121.  
  122. __END__
  123.  
  124. =back
  125.  
  126. =head1 AUTHOR
  127.  
  128. Daniel P. Berrange.
  129.  
  130. =head1 COPYRIGHT
  131.  
  132. Copyright (C) 2004-2006 Daniel P. Berrange
  133.  
  134. =head1 SEE ALSO
  135.  
  136. L<Net::DBus::Reactor>
  137.  
  138. =cut
  139.  
  140.